package com.bookstore.spring.dao;


import com.bookstore.spring.pojo.Book;
import com.bookstore.spring.pojo.ShoppingCartItem;
import com.bookstore.spring.util.MybatisSessionFactory;
import com.bookstore.spring.web.CriteriaBook;
import com.bookstore.spring.web.Page;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * Created by Admin on 9/16 0016.
 */
@Repository
public class BookMap {
    private String str = "com.bookstore.spring.mapper.BookMapper.";

    /**
     * 根据id 获取指定 Book 对象
     *
     * @param id
     * @return
     */
    public Book getBook(int id) {
        String statement = str + "getBook";
        Book book = MybatisSessionFactory.getSession().selectOne(statement, id);
        return book;
    }

    /**
     * 根据书名返回相同的记录数
     *
     * @param title
     * @return
     */
    public long getCountWithTitle(String title) {
        String statement = str + "getCountWithTitle";
        long count = MybatisSessionFactory.getSession().selectOne(statement, title);
        return count;
    }

    /**
     * 根据传入的CriteriaBook 对象返回对应的 Page 对象
     *
     * @param cb
     * @return
     */
    public Page<Book> getPage(CriteriaBook cb) {
        Page<Book> page = new Page<Book>(cb.getPageNo());
        //根据传入的cb 获取符合条件的总记录数
        //然后设置为符合该记录数
        page.setTotalItemNumber(getTotalBookNumber(cb));
        //校验 pageNo 的合法性
        cb.setPageNo(page.getPageNo());
        //根据传入的CriteriaBook 和 pageSize 返回当前页对应的List
        page.setList(getPageList(cb, 3));
        return page;
    }

    /**
     * 根据传入的CriteriaBook 对象返回对应的 记录数
     *
     * @param cb
     * @return
     */
    public long getTotalBookNumber(CriteriaBook cb) {
        String statement = str + "getTotalBookNumber";
        long count = MybatisSessionFactory.getSession().selectOne(statement, cb);
        return count;
    }

    /**
     * 根据传入的CriteriaBook 和 pageSize 返回当前页对应的List
     *
     * @param cb
     * @param pageSize
     * @return
     */
    public List<Book> getPageList(CriteriaBook cb, int pageSize) {
        String statement = str + "getPageList";
        Map<String, Object> map = new HashMap<>();
        map.put("minPrice", cb.getMinPrice());
        map.put("maxPrice", cb.getMaxPrice());
        map.put("pageNo", (cb.getPageNo() - 1) * pageSize);
        map.put("pageSize", pageSize);
        List<Book> books = MybatisSessionFactory.getSession().selectList(statement, map);
        return books;
    }

    /**
     * 返回指定id 的book 的storeNumber 字段的值
     *
     * @param id
     * @return
     */
    public int getStoreNumber(Integer id) {
        String statement = str + "getStoreNumber";
        int value = MybatisSessionFactory.getSession().selectOne(statement, id);
        return value;
    }

    public void batchUpdateStoreNumberAndSalesAmount(Collection<ShoppingCartItem> items) {
        String statement = str + "batchUpdateStoreNumberAndSalesAmount";
        Map<String, Object> map1 = new HashMap<>();
        List<ShoppingCartItem> sci = new ArrayList<>(items);
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < items.size(); i++) {
            map.put("salesAmount", sci.get(i).getQuantity());
            map.put("storeNumber", sci.get(i).getQuantity());
            map.put("id", sci.get(i).getBook().getId());
        }
        map1.put("list", sci);
        MybatisSessionFactory.getSession().update(statement, map1);
    }

    public void save(Book book) {
        String statement = str + "save";
        MybatisSessionFactory.getSession().insert(statement, book);
    }
}
